package com.cgq.pro.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.cgq.base.dao.impl.BaseDaoImpl;
import com.cgq.pro.dao.LandDao;
import com.cgq.pro.entity.Land;
import com.cgq.pro.entity.LandForm;

@Repository
public class LandDaoImpl extends BaseDaoImpl<Land> implements LandDao {


	public int getTotal(Map<String, String> map,int type) {
		StringBuffer hql= new StringBuffer();
		hql.append("  from Land  o where o.type="+type+" and o.status=1 ");

		if(map!=null && map.size()>0)
		{
			hql.append(" and o.dateCre>='"+map.get("startDate")+"'");
			hql.append(" and o.dateCre<='"+map.get("endDate")+"'");

			if(!"-1".equals(map.get("regionId").toString())){
				hql.append(" and o.region.id='"+map.get("regionId").toString()+"' ");
			}   
		}
		List list = hibernateTemplate.find(hql.toString());

		if(list!=null && list.size()>0)
			return list.size();
		else 
			return 0;
	}

	public List<Land> getAllRecords(final int start, final int pageSize, Map<String, String> map, int type) {

		final StringBuffer sb=new StringBuffer();
		sb.append(" from Land o where o.type="+type );

		if(map!=null)
		{
			sb.append(" and o.dateCre>='"+map.get("startDate")+"'");
			sb.append(" and o.dateCre<='"+map.get("endDate")+"'");

			if(!"-1".equals(map.get("regionId").toString())){
				sb.append(" and o.region.id='"+map.get("regionId").toString()+"' ");
			}     
		}
		List list = null;
		list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(start);
				query.setMaxResults(pageSize);
				return query.list();
			}

		});
		return list;
	}

	public List<Land> getAllRecords(LandForm landForm) {
		StringBuffer sb=new StringBuffer();
		sb.append(" from Land l where 1=1 ");
		if(landForm!=null){
			if(landForm.getArea()!=null&&!("-1".equals(landForm.getArea())))
				sb.append(" and l.region.id='"+landForm.getArea()+"' ");
			if(landForm.getLandType()!=null&&!("4".equals(landForm.getLandType())))
				sb.append(" and l.type="+landForm.getLandType()+" ");
		}

		List list=hibernateTemplate.find(sb.toString());

		return list;
	}

}
